package cn.pegasus.teamwork.pegasus.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.iyizhan.teamwork.base.service.impl.BaseServiceImpl;
import cn.iyizhan.teamwork.base.util.PageHelper;
import cn.iyizhan.teamwork.base.util.PageObject;
import cn.pegasus.teamwork.pegasus.entity.BussWasherSgin;
import cn.pegasus.teamwork.pegasus.mapper.BussWasherSginMapper;
import cn.pegasus.teamwork.pegasus.service.BussWasherSginService;
import cn.pegasus.teamwork.util.DateCompareUtil;

/**
 * @author ryan.bian
 * @version
 */
@Service("bussWasherSginService")
public class BussWasherSginServiceImpl extends BaseServiceImpl<BussWasherSgin, BussWasherSginMapper>
		implements BussWasherSginService {
	// 注入当前dao对象
	@Autowired
	private BussWasherSginMapper bussWasherSginMapper;

	public BussWasherSginServiceImpl() {
		setMapperClass(BussWasherSginMapper.class, BussWasherSgin.class);
	}

	@Override
	public PageObject<BussWasherSgin> PageList(Map<String, Object> map) {
		int totalData = bussWasherSginMapper.getTotal(map);
		PageHelper pageHelper = new PageHelper(totalData, map);
		List<BussWasherSgin> list = bussWasherSginMapper.findListByPage(pageHelper.getMap());
		PageObject<BussWasherSgin> pageObject = pageHelper.getPageObject();
		pageObject.setDataList(list);
		return pageObject;
	}

	@Override
	public Map<String, Object> getWasherSginList(Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Date nowDate = new Date();
		Date senDate = DateCompareUtil.getNextDate(nowDate);
		Date triDate = DateCompareUtil.getNextDate(senDate);

		int washerId = Integer.valueOf(map.get("washerId").toString());

		BussWasherSgin bs = new BussWasherSgin();
		bs.setWasherId(washerId);
		bs.setSginDate(DateCompareUtil.getDateToDateString(nowDate));
		String morning = "";
		String afternoon = "";
		bs = bussWasherSginMapper.selectSingle(bs);
		if (bs != null) {
			if (bs.getSginMorning() != null) {
				morning = "1";
			} else {
				morning = "0";
			}
			if (bs.getSginAfternoon() != null) {
				afternoon = "1";
			} else {
				afternoon = "0";
			}
		} else {
			morning = "0";
			afternoon = "0";
		}
		resultMap.put(DateCompareUtil.getDateToDateString(nowDate), morning+ "," + afternoon);
		
		BussWasherSgin bs2 = new BussWasherSgin();
		bs2.setWasherId(washerId);
		bs2.setSginDate(DateCompareUtil.getDateToDateString(senDate));
		String morning2 = "";
		String afternoon2 = "";
		bs2 = bussWasherSginMapper.selectSingle(bs2);
		if (bs2 != null) {
			if (bs2.getSginMorning() != null) {
				morning2 = "1";
			} else {
				morning2 = "0";
			}
			if (bs2.getSginAfternoon() != null) {
				afternoon2 = "1";
			} else {
				afternoon2 = "0";
			}
		} else {
			morning2 = "0";
			afternoon2 = "0";
		}
		resultMap.put(DateCompareUtil.getDateToDateString(senDate), morning2+ "," + afternoon2);
		
		BussWasherSgin bs3 = new BussWasherSgin();
		bs3.setWasherId(washerId);
		bs3.setSginDate(DateCompareUtil.getDateToDateString(triDate));
		String morning3 = "";
		String afternoon3 = "";
		bs3 = bussWasherSginMapper.selectSingle(bs3);
		if (bs3 != null) {
			if (bs3.getSginMorning() != null) {
				morning3 = "1";
			} else {
				morning3 = "0";
			}
			if (bs3.getSginAfternoon() != null) {
				afternoon3 = "1";
			} else {
				afternoon3 = "0";
			}
		} else {
			morning3 = "0";
			afternoon3 = "0";
		}
		resultMap.put(DateCompareUtil.getDateToDateString(triDate), morning3 + "," + afternoon3);
		return resultMap ;
	}

	/**
	 * 洗车工签到
	 */
	public int updateWasherSginList(Map<String, Object> map) {
		String value = map.get("params").toString();
		int washerId = Integer.valueOf(map.get("washerId").toString());
		if (!StringUtils.isEmpty(value)) {
			String[] vs = value.split(";");
			if (vs != null && vs.length > 0) {
				for (String v : vs) {
					String[] vvs = v.split(",");
					String times = vvs[0];
					String morning = vvs[1];
					String afternoon = vvs[2];
					BussWasherSgin bs = new BussWasherSgin();
					bs.setWasherId(washerId);
					bs.setSginDate(times);
					bs = bussWasherSginMapper.selectSingle(bs);
					if (bs != null) {
						if (morning.equals("1")) {
							bs.setSginMorning("9:00-13:00");
						}
						if (afternoon.equals("1")) {
							bs.setSginAfternoon("14:00-21:00");
						}

						bussWasherSginMapper.updateByEntity(bs);
					} else {
						bs = new BussWasherSgin();
						if (morning.equals("1")) {
							bs.setSginMorning("9:00-13:00");
						}
						if (afternoon.equals("1")) {
							bs.setSginAfternoon("14:00-21:00");
						}
						bs.setWasherId(washerId);
						bs.setSginDate(times);
						bussWasherSginMapper.insertByEntity(bs);
					}
				}

				return 0;
			} else {
				return 1;
			}
		}
		return -1;
	}

}
